var apiUrl = $("#apiUrl").val();
var repairId = location.search.split("=")[1];
$(document).ready(function (){
	menuCss("维修");
	getDetail(repairId);
})
var partMap = new Map();
function getDetail(id){
	publicAjax(apiUrl+"repair/details","POST",{repairId: id},function (data) {
		console.log(data);
		if(data.ret == 200){
			$("#repairTime").text(data.data.repairTime || '');
			$("#repairNo").text(data.data.repairNo || '');
			$("#repairPerson").text(data.data.repairPerson || '');
			$("#repairPersonPhone").text(data.data.repairPersonPhone || '');
			$("#faultDepictHospital").text(data.data.faultDepictHospital || '');
			$("#urgentDegree").text(data.data.urgentStr || '');
			$("#faultTime").text(data.data.faultTime || '');
			$("#deviceStatus1").text(data.data.statusStr || '');
			$("#handleTime").text(data.data.handleTime || '');
			$("#repairModeStr").text(data.data.repairModeStr || '');
			$("#dispathTime").text(data.data.dispatchTime || '');
			$("#hospitalContacts").text(data.data.repairPerson || '');
			$("#hospitalPhone").text(data.data.repairPersonPhone || '');
			$("#projectChargePerson").text(data.data.engineerName || '');
			$("#projectChargePersonPhone").text(data.data.mobile || '');
			$("#repairDeviceStartStatus").text(data.data.deviceStatusStr || '');
			$("#repairContent").text(data.data.repairContent || '');
			$("#faultDepictEngineer").text(data.data.faultDepictEngineer || '');
			$("#travelTime").text(data.data.travelTime || '');
			$("#repairDeviceEndStatus").text(data.data.endDeviceStatusStr || '');
			$("#repairEndTime").text(data.data.repairEndTime || '');
			$("#hospitalConfirmTime").text(data.data.hospitalConfirmTime || '');
			$("#hospitalConfirmPerson").text(data.data.hospitalConfirmUsername || '');
			$("#remark").text(data.data.remark || '');
			if(data.data.deviceId){
				$("#deviceName").html("<a target='_blank' href='../device/info?id="+data.data.deviceId+"'>"+(data.data.deviceName || '')+"</a>")
			}
			$("#deviceModel").text(data.data.deviceModel || '');
			$("#sn").text(data.data.sn || '');
			var str = '';
			if(data.data.eventList && data.data.eventList.length){
				$.each(data.data.eventList,function (index,item) {
		            var classs="fa fa-clock-o";
		            var color="#5082eb";
		            switch (item.type){
		                case 1700 :classs="fa fa-wrench";
		                    color = "#f9b229";
		                    break;
		                case 1701 :classs="fa fa-cogs";
		                    color = "#82ca2b";
		                    break;
		                case 1702 :classs="fa fa-rocket";
		                    color = "#10458f";
		                    break;
		                default : classs="fa fa-clock-o";
		                    color="#5082eb";
		            }

		            str+="<li><i class='"+classs+"' style='color: #fff;background:"+color+"'></i>"
		                +"<div class='timeline-item'>"
		                +"<h3 class='timeline-header' style='background: #fff'>"
		                +"<span style='color: #000'>"+(item.eventDateTime==null?'':item.eventDateTime)+"</span>"
		                +"</h3>"
		                +"<div class='timeline-body'>"
		                +"<span style='color: #5082eb'>"+(item.eventTitle==null?'': '【'+item.eventTitle+'】 ')+"</span>"
		                +item.eventContent
		                +"</div>"
		                +"<div>";
		            if(item.fileList != null && item.fileList.length > 0){
		            	var imgType = ["jpg", "png", "jpeg", "gif","JPG","JPEG", "PNG", "GIF"];
		                $.each(item.fileList,function (index,items) {
		                	if(imgType.indexOf(items.fileSuffix) > -1){
		                		str+= "<img class='imgList' style = 'width:100px;height:30px;' src='"+items.fileUrl+"' alt='Visa'> &nbsp;";
		                	}else{
		                		str+= "<p style='padding-left: 10px'><a target='_blank' style='cursor: pointer' href='"+items.fileUrl+"'>"+items.fileName+"</a></p>"
		                	}
		                })
		            }
		            str+="</div>"
		                +"</div>"
		                +"</li>";
		        });
		        str+="<li><i class='fa fa-clock-o bg-gray'></i></li>";
		        $("#timeAxis").html(str);
				$(".imgList").viewer("destroy");
		        $(".imgList").viewer();
			}else{
				str = "暂无事件";
        		$("#timeline").html(str);
			}



			if(data.data.voice == ''){
				$('#IsHidden').hide();
			}else {
				//音频播放
				$('#Audio_Box').attr('src',data.data.voice)
			}





			//指派信息
			if(data.data.appointList && data.data.appointList.length){
				$("#appointTable").html("");
				$.each(data.data.appointList,function (index,item){
					$("#appointTable").append("<tr>\
						<td class='text-muted'>"+(index+1)+"</td>\
						<td class='text-muted'>"+(item.instructName || '')+"</td>\
						<td class='text-muted'>"+(item.instructTypeName || '')+"</td>\
						<td class='text-muted'>"+(item.instructedName || '')+"</td>\
						<td class='text-muted'>"+(item.instructedTypeName || '')+"</td>\
						<td class='text-muted'>"+(item.engineerName || '')+"</td>\
						<td class='text-muted'>"+(item.engineerPhone || '')+"</td>\
					</tr>")
				})
			}

			//故障图片
			/*if(data.data.repairFileList && data.data.repairFileList.length){
				$("#image-td").html("");
				$.each(data.data.repairFileList,function (index,item) {
					$("#image-td").append("<img id='img-"+index+"' style='margin: 5px; max-width: 250px;height: 150px' title='"+item.fileName+"' src='"+item.fileUrl+"' alt='"+item.fileName+"'>");
					$("#img-"+index).viewer("destroy");
		        	$("#img-"+index).viewer();
				})
			}*/
			$("#repairFileTable").html("");
		    var strs = "";
		    if(data.data.fileList && data.data.fileList.length){
		        $.each(data.data.fileList,function (index,item) {
		            var fileName = item.fileName + "." + item.fileSuffix;
		            strs += "<tr><td class='text-muted text-center'>"+(index+1)+"</td>" +
		                "<td class='text-muted text-center'>"+(item.fileTypeName == null?'':item.fileTypeName)+"</td>" +
		                "<td class='text-muted text-center'><a href='javascript:showFile(\""+item.fileUrl+"\",\""+fileName+"\")'>"+(item.fileName == null?'':item.fileName)+"</a></td>" +
		                "<td class='text-muted text-center'><button type='button' class='btn btn-danger btn-xs' title='下载' onclick='fileDownload(\""+item.fileName+"\",\""+item.fileUrl+"\",\""+item.fileSuffix+"\")'><i class='fa fa-fw fa-download'></i></button></td>" +
		                "</tr>";
		        });
		    }else{
		        strs = "<tr><td colspan='10' class='text-muted text-center'>暂无文件</td></tr>";
		    }
		    $("#repairFileTable").html(strs);

		    //绑定备件
		    $("#repair_part_List").html("");
		    var str1 = "";
		    if(data.data.partList && data.data.partList.length){
		        $.each(data.data.partList,function (index,item) {
		        	var oldFileLength = item.oldFileList == null ? 0 : item.oldFileList.length;
            		var newFileLength = item.newFileList == null ? 0 : item.newFileList.length;
		            str1 += "<tr>" +
		                "<td class='text-muted'>"+(item.partExplainName == null?'':item.partExplainName)+"</td>" +
		                "<td class='text-muted'>"+(item.newPartName == null?'':item.newPartName)+"</td>" +
		                "<td class='text-muted'>"+(item.newPartBrand == null?'':item.newPartBrand)+"</td>" +
		                "<td class='text-muted'>"+(item.newPartModel == null?'':item.newPartModel)+"</td>" +
		                "<td class='text-muted'>"+(item.newPartSn == null?'':item.newPartSn)+"</td>" +
		                "<td class='text-muted text-right'>"+(item.newPartPrice == null?'':item.newPartPrice/100)+"</td>" +
		                "<td class='text-muted'>"+(item.oldPartName == null?'':item.oldPartName)+"</td>" +
		                "<td class='text-muted'>"+(item.oldPartBrand == null?'':item.oldPartBrand)+"</td>" +
		                "<td class='text-muted'>"+(item.oldPartModel == null?'':item.oldPartModel)+"</td>" +
		                "<td class='text-muted'>"+(item.oldPartSn == null?'':item.oldPartSn)+"</td>" +
		                "<td class='text-muted text-right'>"+(item.oldPartPrice == null?'':item.oldPartPrice/100)+"</td>" +
		                "<td class='text-muted text-center'><a href='javascript:showPartFile("+item.partId+",1)'>"+oldFileLength+"</a></td>" +
                		"<td class='text-muted text-center'><a href='javascript:showPartFile("+item.partId+",2)'>"+newFileLength+"</a></td>" +
		                "</tr>";
		                partMap.put(item.partId,item);
		        });
		        $("#repair_part_List").html(str1);
		    }else{
		        str1 = "<tr><td colspan='17' class='text-muted text-center'>暂无备件信息</td></tr>"
		    }
		    $("#scoreTable").html('<tr><td class="text-center" colspan="2">暂无评分信息</td></tr>');
		    if(data.data.hospitalCommentView && data.data.hospitalCommentView.commentInfoList.length){
		    	$("#scoreTable").html("");
		    	$.each(data.data.hospitalCommentView.commentInfoList,function (index,item) {
		    		$("#scoreTable").append("<tr>\
		    			<td>"+item.dividTypeName+"</td>\
		    			<td>"+item.value+"分（"+scoreText(item.value)+"）</td>\
		    		</tr>");
		    	})
		    	$("#scoreTable").append("<tr>\
	    			<td>评论</td>\
	    			<td>"+data.data.hospitalCommentView.commentContent+"</td>\
	    		</tr>");
		    }
		}
	})
}

$("#showFileDiv").iziModal({
    title: "备件图片",
    headerColor: "#00c0ef",
    overlayClose:true,
    autoOpen:false,
    history:false,
    zindex: 9999,
    width:800,
    focusInput: false,
    bodyOverflow: true,
    fullscreen: true,
    openFullscreen: true,
    closeOnEscape: true,
    overlay: true,
    overlayClose: false,
    onOpening: function () {},
    onOpened: function () {},
    onClosing: function () {},
    onClosed: function () {}
});

function showPartFile(id,type){
    $("#oldFileImgDiv").html("");
    $("#newFileImgDiv").html("");
    var fileList;
    if(type == 1){
        fileList = partMap.get(id).oldFileList;
    }else{
        fileList = partMap.get(id).newFileList;
    }
    if(fileList == null){
        return;
    }else{
        $.each(fileList,function (index,item){
            if(type == 1){
                $("#oldFileImgDiv").append("<img style='width:45%' height='400px;' src='"+item.fileUrl+"'/>");
                $("#newFileDiv").hide();
                $("#oldFileDiv").show();
            }else{
                $("#newFileImgDiv").append("<img style='width:45%' height='400px;' src='"+item.fileUrl+"'/>");
                $("#oldFileDiv").hide();
                $("#newFileDiv").show();
            }
        });
    }

    $("#showFileDiv").find("img").viewer("destroy");
    $("#showFileDiv").find("img").viewer();
    $("#showFileDiv").iziModal("open",this);
}

function fileDownload(fileName,filePath,fileSuffix){
    location.href= apiUrl+"/file/fileDownload?filePath="+filePath+"&fileName="+fileName;
}

function showFile(url,file){
    $("#file_show_modal").iziModal({
        zindex:999999,
        headerColor: '#00c0ef',
        title: '预览',
        subtitle: file,
        overlayClose: true,
        history:false,
        iframe : true,
        iframeURL:url,
        fullscreen: true,
        openFullscreen: false,
        onClosed:function(){
            $('#file_show_modal').iziModal('destroy');
        }
    });
    $("#file_show_modal").iziModal('open');
}